/* A simple synchronous XML-RPC client written in C, as an example of
   an Xmlrpc-c client.  This invokes the sample.add procedure that the
   Xmlrpc-c example server.c server provides.  I.e. it adds to numbers
   together, the hard way.
*/

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#include <xmlrpc-c/base.h>
#include <xmlrpc-c/client.h>

#include <xmlrpc-c/config.h>  /* information about this build environment */

#define NAME "Xmlrpc-c Test Client"
#define VERSION "1.0"

static void 
die_if_fault_occurred (xmlrpc_env * const envP) {
    if (envP->fault_occurred) {
        fprintf(stderr, "XML-RPC Fault: %s (%d)\n",
                envP->fault_string, envP->fault_code);
        exit(1);
    }
}



int 
main(int           const argc, 
     const char ** const argv __attribute__((__unused__))) {

    xmlrpc_env env;
    xmlrpc_value * resultP;
    xmlrpc_int32 sum;
    int i;
    //const char * const serverUrl = "http://localhost:8081/RPC2";
    char **serverUrls;
    const char * const methodName = "sample.add";
    int semantic, server;
	server=5;
    semantic=((server/2)+1);//atoi(argv[1]);

    if (argc-1 > 0) {
        fprintf(stderr, "This program has no arguments\n");
        exit(1);
    }

    /* Initialize our error-handling environment. */
    xmlrpc_env_init(&env);

    /* Start up our XML-RPC client library. */
    xmlrpc_client_init2(&env, XMLRPC_CLIENT_NO_FLAGS, NAME, VERSION, NULL, 0);
    die_if_fault_occurred(&env);
		//multirpc_server_info multiinfo;
        //multiinfo.numberOfServers = 5;
        serverUrls = malloc(sizeof(char *) * 5);
        serverUrls[0] = "http://localhost:8080/RPC2";
        serverUrls[1] = "http://localhost:8081/RPC2";
        serverUrls[2] = "http://localhost:8082/RPC2";
        serverUrls[3] = "http://localhost:8083/RPC2";
        serverUrls[4] = "http://localhost:8084/RPC2";
//for(i=0;i<5;i++)
{
	printf("Making syncXMLRPC call to "
           "to request the sum "
           "of 5 and 7\n");
    /*printf("Making syncXMLRPC call to server url '%s' method '%s' "
           "to request the sum "
           "of 5 and %d...\n", serverUrls[i], methodName,i);*/

        /* Make the remote procedure call */

    xmlrpc_client_call_sync(semantic, &env, serverUrls, methodName,
                                 "(ii)", (xmlrpc_int32) 5,(xmlrpc_int32) 7);

    //xmlrpc_read_int(&env, resultP, &sum);

    //die_if_fault_occurred(&env);
    //printf("The sum is %d\n", sum);
}
    

    printf("Call complete\n");
    /* Dispose of our result value. */
    //xmlrpc_DECREF(resultP);
    /* Clean up our error-handling environment. */
    xmlrpc_env_clean(&env);
    /* Shutdown our XML-RPC client library. */
    xmlrpc_client_cleanup();

    return 0;
}


